﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.IO;
using System.Web;
using System.Threading.Tasks;
using System.Data;
using BartonExtension;
using ResponseObject;
using SQLWrapper;
using Model.App;
using Model.Images;
using Model.Weixin;
using Services.Common;
using Services.Activity;

namespace iShangMai.Controllers
{
    public class ActivityController : ApiController
    {
        [HttpGet]
        public async Task<ZKResponse> GetActivitie(UInt64 uid, UInt64 act_id, string token)
        {
            ZKResponse response = new ZKResponse();
            ISMGetSingleActivitieRequest request = new ISMGetSingleActivitieRequest()
            {
                token = token,
                uid = uid,
                act_id = act_id
            };
            ISMActivityServiceProvider provider = (ISMActivityServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.ACTIVITIE];
            ISMServiceModel model = new ISMServiceModel()
            {
                userinfo = request,
                operationIdentifier = ISMActivityServiceProvider.GET_SINGLE_ACT
            };
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }
        public async Task<ZKResponse> GetActivities(UInt64 uid,int page,int pageSize,string token)
        {
            ZKResponse response = new ZKResponse();
            ISMGetActivitiesRequest request = new ISMGetActivitiesRequest()
            {
                page = page,
                pageSize = pageSize,
                token = token,
                uid = uid
            };
            ISMActivityServiceProvider provider = (ISMActivityServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.ACTIVITIE];
            ISMServiceModel model = new ISMServiceModel()
            {
                operationIdentifier = ISMActivityServiceProvider.GET_ACTS,
                userinfo = request
            };
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }
        [HttpGet]
        public async Task<ZKResponse> GetActivitieSubscribers(UInt64 aid,int page,int pageSize,string token)
        {
            ZKResponse response = new ZKResponse();
            ISMGetActivitieSubsRequest request = new ISMGetActivitieSubsRequest()
            {
                aid = aid,
                token = token,
                page = page,
                pageSize = pageSize
            };
            ISMActivityServiceProvider provider = (ISMActivityServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.ACTIVITIE];
            ISMServiceModel model = new ISMServiceModel()
            {
                userinfo = request,
                operationIdentifier = ISMActivityServiceProvider.GET_SUBS
            };
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }
        [HttpPost]
        public async Task<ZKResponse> AddActivity(ISMAddActivityRequest request)
        {
            ZKResponse response = new ZKResponse();
            ISMActivityServiceProvider provider = (ISMActivityServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.ACTIVITIE];
            ISMServiceModel model = new ISMServiceModel()
            {
                operationIdentifier = ISMActivityServiceProvider.ADD_ACTS,
                userinfo = request
            };
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }
        [HttpGet]
        public async Task<ZKResponse> ActivityOps(UInt64 aid,string state,string op,string token)
        {
            ZKResponse response = new ZKResponse();
            ISMActivitieOperationRequest request = new ISMActivitieOperationRequest()
            {
                aid = aid,
                op = op,
                state = state,
                token = token
            };
            switch(op.ToLower())
            {
                case "cancel":
                    {
                        ISMActivityServiceProvider provider = (ISMActivityServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.ACTIVITIE];
                        ISMServiceModel model = new ISMServiceModel()
                        {
                            operationIdentifier = ISMActivityServiceProvider.CANCEL_ACTS,
                            userinfo = request
                        };
                        var result = await provider.doProcess(model);
                        response.isProcessSuccess = result.isOperationSuccess;
                        response.errMsg = result.errorMsg;
                        response.userinfo = result.dbReturningState;
                    }
                    break;
                case "get_required_form":
                    {
                        ISMActivityServiceProvider provider = (ISMActivityServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.ACTIVITIE];
                        ISMServiceModel model = new ISMServiceModel()
                        {
                            operationIdentifier = ISMActivityServiceProvider.GET_ACT_REQUIRED_FORM,
                            userinfo = request
                        };
                        var result = await provider.doProcess(model);
                        response.isProcessSuccess = result.isOperationSuccess;
                        response.errMsg = result.errorMsg;
                        response.userinfo = result.dbReturningState;
                    }
                    break;
                case "sub_activity":
                    {
                        ISMActivityServiceProvider provider = (ISMActivityServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.ACTIVITIE];
                        ISMServiceModel model = new ISMServiceModel()
                        {
                            operationIdentifier = ISMActivityServiceProvider.SUB_ACT,
                            userinfo = request
                        };
                        var result = await provider.doProcess(model);
                        response.isProcessSuccess = result.isOperationSuccess;
                        response.errMsg = result.errorMsg;
                        response.userinfo = result.dbReturningState;
                    }
                    break;
                default:
                    {
                        response.isProcessSuccess = false;
                        response.errMsg = "未知命令";
                    }
                    break;
            }
            return response;
        }
    }
}